//
//  ChangePassword_VC.m
//  SpendMoney
//
//  Created by  lixinxin on 16/12/28.
//  Copyright © 2016年  lixinxin. All rights reserved.
//

#import "ChangePassword_VC.h"

@interface ChangePassword_VC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *oldPwd;
@property (weak, nonatomic) IBOutlet UITextField *nPwd;
@property (weak, nonatomic) IBOutlet UITextField *repeatPwd;
@property (weak, nonatomic) IBOutlet UIButton *reviseBtn;

@end

@implementation ChangePassword_VC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    

    self.navigationItem.title = @"修改密码";
    
    self.oldPwd.delegate = self;
    self.nPwd.delegate = self;
    self.repeatPwd.delegate = self;
    [self.reviseBtn setBackgroundColor:MAIN_COLOR];
    
}
#pragma mark - 字数限制
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // 密码和重复密码
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容

    if (toBeString.length > 16) {
        return NO;
    }
    return YES;
}
- (IBAction)changePwd:(UIButton *)sender {
    
    if ([self.oldPwd.text isEqualToString:@""]) {
        [[Toast makeText:@"输入原始密码不能为空"] show];
        return;
    }
    if ([self.nPwd.text isEqualToString:@""] || [self.repeatPwd.text isEqualToString:@""]) {
        [[Toast makeText:@"输入新密码不能为空"] show];
        return;
    }
    if (![self.nPwd.text isEqualToString:self.repeatPwd.text]) {
        [[Toast makeText:@"输入两次密码不一致"] show];
        return;
    }
    
    if (self.nPwd.text.length > 16 || self.nPwd.text.length < 4) {
        [[Toast makeText:@"请您输入4—16位数密码"] show];
        
        return;
    }
    
    
    
    // 提交申请
    [self confirmMethod:sender];
    

    
}

- (void)confirmMethod:(UIButton *)sender
{
    
    
    NSString *userID = [sharBaseTool decrypt:User_ID];
    NSString *userPhone = [sharBaseTool decrypt:User_Phone];
    
    if (!userID) {
        [[Toast makeText:@"请先登录！"] show];
        return;
    }
    sender.enabled = NO;
    
    [Network link:kResetPassword
       parameters:@{@"USERNAME":userPhone,
                    @"PASSWORD":_nPwd.text,
                    @"OLDPASSWORD"   :_oldPwd.text,
                    @"USER_ID"    :userID}
        outOfTime:0.f
         callback:^(Network *response) {
             
             sender.enabled = YES;
             if (response.responCode==1) {
                 
                 // 返回到首页
                 [self.navigationController popViewControllerAnimated:YES];
                 
             }
             [[Toast makeText:response.dataDic[@"result"]] show];
             
             //             [self alertMessage:response.dataDic[@"result"] title:@"提示" forAnimation:nil];
             
         }];
    
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
